<?php

namespace app\admin\model;

use think\Model;
use think\model\concern\SoftDelete;

/**
 * Category
 * @controllerUrl 'category'
 */
class Category extends Model
{
    use SoftDelete;

    // 表名
    protected $name = 'category';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';


    public function getCategoryIdAttr($value, $row)
    {
        return !$value ? '' : $value;
    }

    public function setSwitchAttr($value, $row)
    {
        return $value ? '1' : '0';
    }

    public function getTree($ids)
    {
        $tree = [];
        foreach ($ids as $id) {
            $tree[] = $id;
            $childIds = $this->where('category_id', $id)->column('id');
            $childIds = $this->getTree($childIds);
            $tree = array_merge($tree, $childIds);
        }
        return $tree;
    }
}